{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #K Items With the Maximum Sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: kItemsWithMaximumSum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #K 件物品的最大和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>袋子中装有一些物品，每个物品上都标记着数字 <code>1</code> 、<code>0</code> 或 <code>-1</code> 。</p>\n",
    "\n",
    "<p>给你四个非负整数 <code>numOnes</code> 、<code>numZeros</code> 、<code>numNegOnes</code> 和 <code>k</code> 。</p>\n",
    "\n",
    "<p>袋子最初包含：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>numOnes</code> 件标记为 <code>1</code> 的物品。</li>\n",
    "\t<li><code>numZeros</code> 件标记为 <code>0</code> 的物品。</li>\n",
    "\t<li><code>numNegOnes</code> 件标记为 <code>-1</code> 的物品。</li>\n",
    "</ul>\n",
    "\n",
    "<p>现计划从这些物品中恰好选出 <code>k</code> 件物品。返回所有可行方案中，物品上所标记数字之和的最大值。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>袋子中的物品分别标记为 {1, 1, 1, 0, 0} 。取 2 件标记为 1 的物品，得到的数字之和为 2 。\n",
    "可以证明 2 是所有可行方案中的最大值。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>袋子中的物品分别标记为 {1, 1, 1, 0, 0} 。取 3 件标记为 1 的物品，1 件标记为 0 的物品，得到的数字之和为 3 。\n",
    "可以证明 3 是所有可行方案中的最大值。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>0 &lt;= numOnes, numZeros, numNegOnes &lt;= 50</code></li>\n",
    "\t<li><code>0 &lt;= k &lt;= numOnes + numZeros + numNegOnes</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [k-items-with-the-maximum-sum](https://leetcode.cn/problems/k-items-with-the-maximum-sum/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [k-items-with-the-maximum-sum](https://leetcode.cn/problems/k-items-with-the-maximum-sum/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['3\\n2\\n0\\n2', '3\\n2\\n0\\n4']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        return sum(sorted([1] * numOnes + [0] * numZeros + [-1] * numNegOnes, reverse=True)[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k<=numOnes+numZeros:\n",
    "            return min(k,numOnes)\n",
    "        return 2*numOnes-k+numZeros\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k<=numOnes:\n",
    "            return k\n",
    "        elif k<=numOnes+numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes+numOnes+numZeros-k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes + numZeros:\n",
    "            return min(k, numOnes)\n",
    "        else:\n",
    "            return numOnes + numOnes + numZeros - k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "\n",
    "        if k<= numOnes:\n",
    "            return  k\n",
    "        elif k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        else :\n",
    "            return numOnes - (k - numOnes - numZeros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        arr = [1]*numOnes+[0]*numZeros+[-1]*numNegOnes\n",
    "        return sum(arr[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= 0:\n",
    "            return 0\n",
    "        elif k <= numOnes and k > 0:\n",
    "            return k\n",
    "        elif k <= numZeros+numOnes and k > numOnes:\n",
    "            return numOnes\n",
    "        elif k <= numNegOnes+numZeros+numOnes and k > numZeros+numOnes:\n",
    "            return 2*numOnes+numZeros-k\n",
    "        else:\n",
    "            return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        \n",
    "        if (k > numOnes) & (k <= numOnes + numZeros):\n",
    "            return numOnes\n",
    "        \n",
    "        if k > numOnes + numZeros:\n",
    "            return numOnes - (k - numOnes - numZeros) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        bigest = 0\n",
    "        for l in range(k):\n",
    "            if numOnes > 0:\n",
    "                bigest += 1\n",
    "                numOnes -= 1\n",
    "            elif numZeros > 0:\n",
    "                numZeros -= 1\n",
    "            else:\n",
    "                bigest -= 1\n",
    "                numNegOnes -= 1\n",
    "        return bigest\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k<=numOnes:\n",
    "            return k\n",
    "        remain = k - numOnes\n",
    "        if remain<=numZeros:\n",
    "            return numOnes\n",
    "        return numOnes - (remain - numZeros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k<=numOnes:\n",
    "            return k\n",
    "        elif k<=numOnes+numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return 2*numOnes+numZeros-k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "      if k <= numOnes:\n",
    "         return k\n",
    "      if numOnes < k <= (numOnes + numZeros):\n",
    "         return numOnes\n",
    "      return numOnes - (k - numOnes - numZeros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        count=0\n",
    "        if k<=numOnes:\n",
    "            count+=k\n",
    "            return count\n",
    "        else:\n",
    "            count+=numOnes\n",
    "            k-=numOnes\n",
    "            if k<=numZeros:\n",
    "                return count\n",
    "            else:\n",
    "                k-=numZeros\n",
    "                count-=k\n",
    "                return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        ans=0\n",
    "        while k>0:\n",
    "            if numOnes>0:\n",
    "                numOnes-=1\n",
    "                ans+=1\n",
    "                k-=1\n",
    "            elif numZeros>0:\n",
    "                numZeros-=1\n",
    "                k-=1\n",
    "            elif numNegOnes>0:\n",
    "                numNegOnes-=1\n",
    "                ans-=1\n",
    "                k-=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        elif k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes - (k - numOnes - numZeros)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "\n",
    "        if numOnes + numZeros >= k:\n",
    "            return min(numOnes, k)\n",
    "        else:\n",
    "            return numOnes - (k-numOnes-numZeros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k \n",
    "        if k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        return numOnes - (k - numOnes - numZeros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= 0:\n",
    "            return 0\n",
    "        else:\n",
    "            if k <= numOnes:\n",
    "                return k\n",
    "            elif (k > numOnes and k - numOnes <= numZeros):\n",
    "                return numOnes\n",
    "            else:\n",
    "                return numOnes - (k - numOnes - numZeros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        \n",
    "        if k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        return numOnes - k + numOnes + numZeros"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k -numOnes <= 0:\n",
    "            return k \n",
    "        else:\n",
    "            if k - numOnes -numZeros <=0 :\n",
    "                return numOnes\n",
    "            else:\n",
    "                return numOnes-(k-numOnes-numZeros)\n",
    "            \n",
    "\n",
    "             \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        k -= numOnes\n",
    "        if k <= numZeros:\n",
    "            return numOnes\n",
    "        k -= numZeros\n",
    "        return numOnes - k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if numOnes >= k: return k\n",
    "        elif numOnes + numZeros >=k: return numOnes\n",
    "        else: return numOnes - k + numOnes + numZeros"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        \n",
    "        ls = [1 for _ in range(numOnes)]+[0 for _ in range(numZeros)]+[-1 for _ in range(numNegOnes)]\n",
    "        ls.sort(reverse = True)\n",
    "        result = ls[0:k]\n",
    "        return sum(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:return k\n",
    "        elif numOnes < k <= numZeros + numOnes:return numOnes\n",
    "        else:\n",
    "            return numOnes - k + numOnes + numZeros "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        ans=[1]*numOnes+[0]*numZeros+[-1]*numNegOnes\n",
    "        return sum(ans[0:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k<=numOnes:\n",
    "            return k\n",
    "        elif k<=numOnes+numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes+numOnes+numZeros-k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        elif k > numOnes and k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        elif k > numOnes + numZeros and k <= numOnes + numZeros + numNegOnes:\n",
    "            return numOnes - (k - numOnes - numZeros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k<=numOnes:\n",
    "            return k\n",
    "        if k<=numOnes+numZeros:\n",
    "            return numOnes\n",
    "        if k<=numOnes+numZeros+numNegOnes:\n",
    "            return numOnes-(k-numOnes-numZeros)\n",
    "        return numOnes-numNegOnes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k \n",
    "        elif k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        else :\n",
    "            return numOnes - (k-(numOnes+numZeros))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if numOnes >= k:\n",
    "            return k\n",
    "        if numZeros >= k - numOnes:\n",
    "            return numOnes\n",
    "        return numOnes - (k - numOnes - numZeros)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k<=numOnes:\n",
    "            return k*1\n",
    "        elif k<=numOnes+numZeros:\n",
    "            return numOnes*1+(k-numOnes)*0\n",
    "        elif k<=numOnes+numZeros+numNegOnes:\n",
    "            return numOnes*1+numZeros*0+(k-numOnes-numZeros)*(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        list1=[]\n",
    "        list1=numOnes*[1]+numZeros*[0]+numNegOnes*[-1]+list1\n",
    "        return sum(list1[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        elif numOnes < k <= numOnes+numZeros:\n",
    "            return numOnes\n",
    "        elif numZeros+numOnes < k <= numNegOnes+numZeros+numOnes:\n",
    "            return numOnes+(k-numOnes-numZeros)*(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        if k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes - (k - numOnes - numZeros)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "\n",
    "        if 0 <= k < numOnes:\n",
    "            return k\n",
    "        elif numOnes <= k < numOnes + numZeros:\n",
    "            return numOnes\n",
    "        elif numOnes + numZeros <= k <= numOnes + numZeros + numNegOnes:\n",
    "            return numOnes - (k - numOnes - numZeros)\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        if k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        return numOnes - (k - numOnes - numZeros)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        elif k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes - (k - (numOnes + numZeros))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int,\n",
    "                             numNegOnes: int, k: int) -> int:\n",
    "        if numOnes >= k:\n",
    "            return k\n",
    "        elif k-numOnes <= numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes - (k-numOnes-numZeros)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k<=numOnes:\n",
    "            return k\n",
    "        elif k<=numOnes+numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes+numOnes+numZeros-k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        return min(numOnes, k) - max(k - numOnes - numZeros, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        sum = 0\n",
    "        sum += min(numOnes, k)\n",
    "        k -= min(numOnes, k)\n",
    "        if k > 0:\n",
    "            k -= min(numZeros,k)\n",
    "        sum -= k\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if numOnes >= k:\n",
    "            return k\n",
    "        elif numOnes < k <= numZeros + numOnes:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes - (k- numZeros - numOnes)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        arr = [1]*numOnes+[0]*numZeros+[-1]*numNegOnes\n",
    "        arr.sort(reverse=True)\n",
    "        # print(arr)\n",
    "        return sum(arr[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k \n",
    "        elif k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes - (k - numOnes - numZeros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        elif numOnes < k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            a = k - (numOnes + numZeros)\n",
    "            return numOnes - a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        res = 0\n",
    "        if k>numOnes:\n",
    "            res = numOnes\n",
    "            k -= numOnes\n",
    "        else:\n",
    "            return k\n",
    "        if k>numZeros:\n",
    "            k -= numZeros\n",
    "        else:\n",
    "            return res\n",
    "        if k>numNegOnes:\n",
    "            return res-numNegOnes\n",
    "        else:\n",
    "            return res - k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        s = 0\n",
    "        if numOnes <= k:\n",
    "            s += numOnes\n",
    "            k -= numOnes\n",
    "        else:\n",
    "            return k\n",
    "        if numZeros <= k:\n",
    "            k -= numZeros\n",
    "        else:\n",
    "            return s\n",
    "        return s - min(k, numNegOnes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        return numOnes - (k - numOnes - numZeros) if k > numOnes + numZeros else min(k, numOnes)\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k<=numOnes:\n",
    "            return k\n",
    "        elif numOnes<k<=numOnes+numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes*2+numZeros-k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        return min(k,numOnes,2*numOnes+numZeros-k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        elif numOnes < k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes - (k - numOnes - numZeros)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        num = []\n",
    "        for i in range(numOnes):\n",
    "            num.append(1)\n",
    "        for i in range(numZeros):\n",
    "            num.append(0)\n",
    "        for i in range(numNegOnes):\n",
    "            num.append(-1)\n",
    "        return sum(num[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        result = 0\n",
    "        if k <= numOnes:\n",
    "            result = k\n",
    "        else:\n",
    "            result = numOnes\n",
    "            if k-numOnes > numZeros:\n",
    "                result -= k-numOnes-numZeros\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k < numOnes:\n",
    "            return k\n",
    "        elif k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return 2 * numOnes + numZeros - k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        elif numOnes < k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes - (k - (numOnes + numZeros))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        return k if k<numOnes else numOnes if k<numOnes+numZeros else 2*numOnes+numZeros-k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k<=numOnes:\n",
    "            return k\n",
    "        elif k<=numOnes+numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes-(k-numOnes-numZeros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "            return min(k,numOnes)+max(0,k-numOnes-numZeros)*-1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if(k<=numOnes):\n",
    "            return k\n",
    "        elif(k<=numOnes+numZeros):\n",
    "            return numOnes\n",
    "        else:\n",
    "            return 2*numOnes-k+numZeros"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        r = 0\n",
    "        for i in range(0,k):\n",
    "            if numOnes > i:\n",
    "                r += 1\n",
    "            elif numOnes + numZeros > i:\n",
    "                r += 0\n",
    "            elif numOnes + numZeros + numNegOnes > i:\n",
    "                r -= 1\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k<=numOnes:\n",
    "            return k\n",
    "        elif k>numOnes and k-numOnes<=numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes-(k-numOnes-numZeros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if numOnes>k:\n",
    "            return 1*k\n",
    "        elif numZeros>(k-numOnes):\n",
    "            return numOnes*1 \n",
    "        else:\n",
    "            return numOnes*1 + (k-numOnes-numZeros)*-1\n",
    "           \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        lst = [1] * numOnes + [0] * numZeros + [-1] * numNegOnes\n",
    "        return sum(lst[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        package=[1]*numOnes+[0]*numZeros+[-1]*numNegOnes\n",
    "        return sum(package[:k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        nums = [1] * numOnes + [0] * numZeros + [-1] * numNegOnes\n",
    "\n",
    "        return sum(nums[:k]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if numOnes >= k:\n",
    "            return k\n",
    "        elif numOnes < k <= numZeros + numOnes:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes - (k- numZeros - numOnes)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        ans=[1]*numOnes+[0]*numZeros+[-1]*numNegOnes\n",
    "        ans.sort(reverse=True)\n",
    "        sum=0\n",
    "        for i in range(k):\n",
    "            sum+=ans[i]\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if numOnes >= k :\n",
    "            return k\n",
    "        elif numZeros >= k-numOnes:\n",
    "            return numOnes\n",
    "        elif numNegOnes >= k-numOnes-numZeros:\n",
    "            return numOnes-(k-numOnes-numZeros)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        res = 0\n",
    "        while k > 0:\n",
    "            if numOnes > 0:\n",
    "                res += 1\n",
    "                numOnes -= 1\n",
    "                k -= 1\n",
    "                continue\n",
    "            if numZeros > 0:\n",
    "                numZeros -= 1\n",
    "                k -= 1\n",
    "                continue\n",
    "            if numNegOnes > 0:\n",
    "                res -= 1\n",
    "                numNegOnes -= 1\n",
    "                k -= 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if numOnes >=k:\n",
    "            return k\n",
    "        if numZeros >= k-numOnes:\n",
    "            return numOnes\n",
    "        return 2*numOnes-k+numZeros"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if(k<=numOnes):\n",
    "            return k\n",
    "        elif(k<=numOnes+numZeros):\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes-(k-numOnes-numZeros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k < numOnes:\n",
    "            return k\n",
    "        elif k < numOnes+numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes- (k-numOnes-numZeros)\n",
    "        #return max(0, )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k<=numOnes:\n",
    "            return k\n",
    "        elif k<=numOnes+numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes-(k-numOnes-numZeros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        nums = [1] * numOnes + [0] * numZeros + [-1] * numNegOnes\n",
    "        return sum(nums[: k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if numOnes >= k:\n",
    "            result=k*1\n",
    "        elif numOnes + numZeros>=k:\n",
    "            result=numOnes*1+(k-numOnes)*0\n",
    "        else:\n",
    "            result=numOnes*1+numZeros*0+(k-numOnes-numZeros)*-1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        total = 0\n",
    "        if k < numOnes:\n",
    "            return k\n",
    "        else:\n",
    "            total = numOnes\n",
    "            k -= numOnes\n",
    "            if k < numZeros:\n",
    "                return total\n",
    "            else:\n",
    "                k -= numZeros\n",
    "                if k <= numNegOnes:\n",
    "                    return total - k\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        elif k <= numOnes + numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes - (k-numOnes-numZeros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n",
    "        if k <= numOnes:\n",
    "            return k\n",
    "        elif k - numOnes <= numZeros:\n",
    "            return numOnes\n",
    "        else:\n",
    "            return numOnes - (k - numOnes - numZeros)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
